import javax.microedition.lcdui.*;
/**
 * 
 * @author Christopher Luttmann
 * @author Alejandro Rojas
 * 
 **/
public class CanvasForm extends Canvas{
    
    private int px = (getWidth()/20)*10;
    private int py = (getHeight()/20)*10;
    private int yr = 10;
    private int xr = 10;
    //matriz de prueba
    private int [][] mapmatrix ;
    
     

    /**
    * constructor
    * Se define la matriz y la pantalla 
    **/
        
    protected CanvasForm(){
        this.setFullScreenMode(false);
        // matriz de prueba
        mapmatrix = new int [21][21];
        for (int i = 0; i<20;i++){
            for (int j = 0; j <20; j ++){ 
                mapmatrix[i][j]= 0;
            }
        }
        mapmatrix[yr][xr] = 2;
    }
    
    /**
     * setPx
     * @param px
     * modificar atributo px
     */
    public void setPx(int px){
        this.px = px;
    }
    /**
     * setPy
     * @param py 
     * modificar atributo py
     */
    public void setPy(int py){
        this.py = py;
    }
    /**
     * setXr
     * @param xr 
     * modificar atributo xr
     */
    public void setXr(int xr){
        this.xr = xr;
    }
    /**
     * setYr
     * @param yr 
     * modificar atributo yr
     */
    public void setYr(int yr){
        this.yr = yr;
    }
    /**
     * setMapmatrix
     * @param mapmatrix 
     * modificar atributo mapmatrix
     */
    public void setMapmatrix(int [][] mapmatrix){
        this.mapmatrix = mapmatrix;
    }
    /**
     * getPx
     * @return px
     * devuelve el valor actual de px
     */
    public int getPx(){
        return px;
    }
    /**
     * getPy
     * @return py
     * devuelve el valor actual de py
     */
    public int getPy(){
        return py;
    }
    /**
     * getYr
     * @return yr 
     * devuelve el valor actual de yr
     */
    public int getYr(){
        return yr;
    }
    /**
     * getXr
     * @return xr
     * devuelve el valor actual de xr
     */
    public int getXr(){
        return xr;
    }
    /**
     * getMapmatrix
     * @return mapmatrix
     * devuelve el valor actual de mapmatrix
     */
    public int [][] getMapmatrix(){
        return mapmatrix;
    }
    /**
     * movAbajo
     * mueve el robot hacia abajo en la pantalla
     */
    public void movAbajo(){
        if(py < (getHeight()/20)*19){
            if(yr < 20){
                if (mapmatrix[yr + 1][xr] != 1){
                    py = py + getHeight()/20;
                    if (mapmatrix[yr][xr]!= 1)
                        mapmatrix[yr][xr] = 3;
                    yr ++;
                    mapmatrix[yr][xr] = 2;
                }
            }
        }
    }
    /**
     * movArriba
     * mueve el robot hacia arriba en la pantalla
     */
    public void movArriba(){
        if(py > 0){
            if(yr > 1  ){
                if (mapmatrix[yr -1][xr] != 1){
                    py = py -getHeight()/20;
                    if (mapmatrix[yr][xr]!= 1)
                        mapmatrix[yr][xr] = 3;
                    yr --;
                    mapmatrix[yr][xr] = 2;
                }
            }
        }
    }
    /**
     * movDerecha
     * mueve el robot hacia arriaba en la pantalla
     */
    public void movDerecha(){
        if(px < (getWidth()/20)*19){
            if( xr <19 ){
                if (mapmatrix[yr][xr+1] != 1){
                    px = px + getWidth()/20;
                    if (mapmatrix[yr][xr]!= 1)
                        mapmatrix[yr][xr] = 3;
                    xr ++;
                    mapmatrix[yr][xr] = 2;
                }
            }
        }
    }
    /**
     * movIzquierda
     * mueve el robot hacia la izaquierda en la pantalla
     */
    public void movIzquierda(){
        if(px > 0){
            if(xr>0 ){
                if (mapmatrix[yr][xr-1] != 1 ){
                    px = px - getWidth()/20; 
                    if (mapmatrix[yr][xr]!= 1)
                        mapmatrix[yr][xr] = 3;
                    xr --;
                    mapmatrix[yr][xr] = 2;
                }
            }
        }
    }
          
          
    /**
    * paint
    * @param g 
    * pinta la pantalla segun las modificaciones realizadas
    */
    
    protected void paint(Graphics g){
        int defcolor = 0;
        int width = getWidth()/20;
        int height = getHeight()/20;
                  
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, width*20, height*20);        
        //inicio del for matrix
     
        for (int f = 0; f<21;f++){
            for (int c = 0; c <20; c ++){
                defcolor = mapmatrix[f][c];
                switch(defcolor){    
                    case 0: // nada fondo
                        g.setColor(0, 0, 0);
                        g.fillRect(width*c, height*(f-1), width, height);
                        break;
                    case 1: //pared
                        g.setColor(40, 100, 50);
                        g.fillRect(width*c, height*(f-1), width, height);
                        break;
                    case 2: // robot
                        px = width*c;
                        py = height*(f-1);
                        g.setColor(255,0,0);
                        g.fillRect(px, py, width, height);
                        break;
                    case 3: // migajas de pan
                        g.setColor(255, 255, 255);
                        g.fillArc(width*c + width/2 , height*(f-1) + height/2, width/4, height/4,0 ,360 );  
                        break;
                 }
            }
        }
        // fin del for matrix   
        for(int h = 1; h < 21; h ++){
            for (int w = 1; w < 21; w ++){
                g.setColor(255, 128, 0);
                g.drawRect(0, 0,w*width,h*height);
            }
        } 
   
   // llamar aqui a genmap
   
                 
    }
      
    /**
     * keyPressed
     * @param keyC 
     * Called when a key is pressed.
    */
    protected void keyPressed(int keyC){
        super.keyPressed(keyC);
        switch(keyC){    
            case Canvas.KEY_NUM8:
                movAbajo();
                break;
            case Canvas.KEY_NUM2:
                movArriba();
                break;
            case Canvas.KEY_NUM6:
                movDerecha();
                break;
            case Canvas.KEY_NUM4:
                movIzquierda();
                break;
         }
         this.repaint();
    }     
     
   
    
    /**
     * keyReleased
     * @param keyC
     * Called when a key is released.
     */
    protected void keyReleased(int keyC) {}

    /**
     * keyRepeated
     * @param keyC 
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyC) {}

    /**
     * pointerDragged
     * @param x, y
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {}

    /**
     * pointerPressed
     * @param x, y
     * Called when the pointer is pressed.
     */
    protected void pointerPressed(int x, int y) {}

    /**
     * pointerReleased
     * @param x, y
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {}

    /**
     * commandAction
     * @param c, d
     * Called when action should be handled
     */
    public void commandAction(Command c, Displayable d) {}
}